<?php
class Literature extends MY_Controller
{
	var $curl = 'literature';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
	var $id_user = '';
	
	function __construct()
	{
		parent::__construct();
		$this->load->model(array('/public/liter_model', '/public/sections_model'));

		$this->validation->set_fields($this->liter_model->fields);
		$this->data['Title'] = T_('Literature');		

		$this->language = parent::getLang();

		$this->name = T_('Publications');
	}
	
	function index()
	{
        $this->liter_model->search();
     	$this->renderLayout();
	}

	function create()
	{
		if (!empty($_POST))
		{
			$rules = array('description','name','author','mark','publishing_house','year');
			
			$this->validation->set_rules($this->liter_model->getRules($rules));
			
			if ($this->validation->run() === TRUE)
			{				
				$item['lit_description']		= $this->validation->description; 
				$item['lit_name']				= $this->validation->name;
				$item['lit_author']				= $this->validation->author;
				$item['lit_mark']				= $this->validation->mark;
				$item['lit_publishing_house']	= $this->validation->publishing_house;
				$item['lit_year']				= $this->validation->year;
				$item['lit_voters']				= ($this->validation->mark=="" ? 0 : 1); 
				
				if($this->validation->subsection) {
					$section	= $this->validation->subsection;
				} else {
					$section	= $this->validation->section;
				}
				
				if ($this->liter_model->insert($item, $section))
       			{	
					redirect($this->curl . '/index'); 
       			}				
			}
		}
		
		$this->data['Title'] .= ' / ' . T_('New publication');
		
		$this->renderLayout();			
	}
	
	
	function edit($id = null)
	{
		if (!empty($id))
		{
			$object = $this->liter_model->getBook($id);
			$section = $this->liter_model->getBookSections($id);
			
			if (!count($_POST))
			{
				$this->validation->id				= $object['lit_id'];
				$this->validation->author			= $object['lit_author']; 
				$this->validation->name				= $object['lit_name'];
				$this->validation->description		= $object['lit_description']; 
				$this->validation->section			= $section['section'];
				$this->validation->subsection		= $section['subsection'];
				$this->validation->publishing_house = $object['lit_publishing_house']; 
				$this->validation->year				= $object['lit_year'];
				$this->validation->mark				= $object['lit_mark'];

				$subsections = $this->liter_model->getSubsections($section['section']);
				foreach($subsections as $row) {
					$subsections_options[$row['section_id']] = $row['section_name'];
				}
				$this->data['subsections_options'] = $subsections_options; 
				
			}else {				
				$rules = array('description','name','author','mark','publishing_house','year');
			
				$this->validation->set_rules($this->liter_model->getRules($rules));
				
				if ($this->validation->run() === TRUE) {
					
					$item['lit_id']					= $this->validation->id;
					$item['lit_author']				= $this->validation->author;
					$item['lit_name']				= $this->validation->name;
					$item['lit_description']		= $this->validation->description; 
					$item['lit_publishing_house']	= $this->validation->publishing_house;
					$item['lit_year']				= $this->validation->year;
					$item['lit_mark']				= $this->validation->mark;
					
					if($this->validation->subsection) {
						$section 	= $this->validation->subsection;
					} else if($this->validation->section){
						$section	= $this->validation->section;
					}
					if(!empty($section)) {
						
						$bookLink = $this->liter_model->getBookLink($item['lit_id']);
					  	$section_data['sections_id'] 	= $section;
				        $section_data['literature_id'] 	= $item['lit_id'];
				        $section_data['litsec_id']		= $bookLink['litsec_id'];
					}
					
					if ($this->liter_model->update($item, $section_data)){
						redirect($this->curl . '/index');
	       			}				
				}
			}	
		}
		$this->data['Title'] .= ' / ' . T_('Edit');
		$this->renderLayout();
	}
	
	function account($id)
	{		
        $sql = "SELECT * 
                FROM stats.users_payments 
                WHERE id_users=".$this->db->escape($id)." 
                ORDER BY id DESC";
        $this->data['history'] = $this->db->query($sql)->result();
        		
		if (count($_POST))
		{			
			
			$rules = array('amount','notes');
			$this->validation->set_fields($this->accountliter_model->fields);
			$this->validation->set_rules($this->accountliter_model->getRules($rules));
			$this->validation->set_message('__positiveNumbers', '<span class="black bold">........');
						

			if ($this->validation->run() === TRUE)
			{			
								
				$item['id_users']		= $id;
				$item['dt']				= date('Y-m-d H:i:s');
				$item['type']			= 0;
				$item['notes']			= $this->validation->notes;	
				
				$item['amount'] = ($_POST['action'] == 'РћС‚РЅСЏС‚СЊ')
				?(-1*$this->validation->amount)
				:(1*$this->validation->amount);
					
						
				if ($this->accountliter_model->insert($item))
				{
					/*$sql = "SELECT nextval('advertisers_id_seq')";
					$sequence = $this->db->query($sql)->row();
					$id_advertiser = $sequence->nextval - 1;
					
					// Save User Roles					
					$this->db->insert('users_roles', array('id_users' => $id_advertiser, 'id_roles' => ROLE_COMPANY, 'id_group' => COMPANY_GROUP_ID));						
				*/	
					//Save User Bonus
			        /* $this->db->insert('stats.advertisers_payments', array('id_advertisers' => $id, 'dt' =>  date('Y-m-d H:i:s'), 'type' => 0, 
										'amount' => $var_amount, 'notes' => $this->validation->notes));
					*/
					redirect($this->curl . '/edit/'.$id);	
				}				
			}
		}
				
		$this->data['Title'] = 'РџРѕРїРѕР»РЅРёС‚СЊ СЃС‡РµС‚';
		$this->validation->id = $id;		
		
		$this->renderLayout();			
	}
	
	function stat($id = 0)
	{				
		if (!empty($id))
		{
			$object = $this->liter_model->get($id);
			$this->data['Title'] = $this->name . ' / ' . T_('Р¤РёРЅР°РЅСЃС‹ Рё СЃС‚Р°С‚РёСЃС‚РёРєР°');					
		}
		
		$this->data['id']   	  = $id;
		$this->data['date_start'] = $this->input->post('date_start');
		$this->data['date_end']   = $this->input->post('date_end');
		
		$this->liter_model->stat($id);
		
		$this->liter_model->table_stat($id);
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
        $data = $this->liter_model->delete($_REQUEST['items']);
        echo $data;
	}

  function delete($id)
  {
	    $data = $this->liter_model->delete($id);
    	redirect($this->curl . '/index');
  }

  function deleteFrItems()
	{
        $data = $this->friends_model->delete($_REQUEST['items']);
        echo $data;
	}

  function deleteFr($id,$user_id)
  {
    $data = $this->friends_model->delete($id);
    redirect($this->curl . '/friends/'.$user_id);
  }


	function activity()
	{
		$data = $this->utils->publishItems($this->liter_model->table, $this->liter_model->prKey, $this->liter_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}
		
  function __loginFree($login = null)
  { 
    return $this->liter_model->loginFree($login);
  }

  function __phoneFree()
  {   
    if (!empty($this->data['ani']))
    {
        foreach ($this->data['ani'] as $phone)
        {
            $res = $this->liter_model->phoneFree($phone);
            if (!$res)
            {
            	$this->validation->set_message('__phoneFree', 'Р’РІРµРґРµРЅРЅС‹Р№ <span class="black bold">«РќРѕР�?РµСЂ С‚РµР»РµС„РѕРЅР°»</span> СѓР¶Рµ Р·Р°РЅСЏС‚ РІ Р±Р°Р·Рµ');            	
                return false;
            }
        }
        
        return true;
    }
    else
    {
        $this->validation->set_message('__phoneNumber', 'РќРµРѕР±С…РѕРґРёР�?Рѕ РґРѕР±Р°РІРёС‚СЊ <span class="black bold">«РќРѕР�?РµСЂ С‚РµР»РµС„РѕРЅР°»</span>');
        return false;
    }
  }
    
  function __phoneValid()
  {   
    if (!empty($this->data['ani']))
    {
        foreach ($this->data['ani'] as $phone)
        {
            if (!preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $phone))
            {
            	echo $phone;
            	$this->validation->set_message('__phoneValid', 'РќРµРІРµСЂРЅС‹Р№ С„РѕСЂР�?Р°С‚ РїРѕР»СЏ <span class="black bold">«РќРѕР�?РµСЂ С‚РµР»РµС„РѕРЅР°»</span>. Р”РѕРїСѓСЃС‚РёР�?С‹Рµ СЃРёР�?РІРѕР»С‹ - С‚РѕР»СЊРєРѕ С†РёС„СЂС‹.');
            }
        }
        
        return true;
    }
  }
  
	function details()
	{
		$id = $this->auth->getID();
		//does the object exist?
		if (!empty($id))
		{
			$object = $this->liter_model->get($id);
			
			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				//passing data
				$this->validation->user_ID 		= $object['user_ID'];
				$this->validation->user_login 	= $object['user_login']; 
				$this->validation->user_name 	= $object['user_name']; 
				$this->validation->user_email 	= $object['user_email'];
				$this->validation->user_phone 	= $object['user_phone'];
				$this->validation->user_password = $object['user_password'];							 
			}
			else
			{
				$rules = array('user_login', 'user_password', 'user_name', 'user_email', 'user_phone');
				$this->validation->set_rules($this->liter_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					//prepare data
					$item['user_ID'] = $this->validation->user_ID;
					$item['user_login'] = $this->validation->user_login;
					$item['user_password'] = $this->validation->user_password;					
					$item['user_name'] = $this->validation->user_name;
					$item['user_email'] = $this->validation->user_email;
					$item['user_phone'] = $this->validation->user_phone;					
					
					$this->liter_model->update($item);
					
					redirect($this->curl . '/index');
				}
			}	
		}
	
		$this->view 	= $this->curl . '/details';
		$this->renderLayout();
	}	
	
	function finance()
	{		
		$id = $this->auth->getID();
		
		if (!empty($id))
		{
			$object = $this->liter_model->get($id);
		}
		
		$this->liter_model->stat($id);
		$this->liter_model->table_stat($id);
		
		$this->data['Title'] = T_('Title');	
		
		$this->renderLayout();
	}
	function send($id) {
		if($id !== null && isset($id)) {
			$this->data['id'] = $id;
			if (!empty($_POST)){
				
				$rules = array('message_header', 'message_text');
				
	            
				$this->validation->set_rules($this->inbox_model->getRules($rules));
				
				if ($this->validation->run() === TRUE)
				{				
					$item['inbox_user_ID'] 			= $id;
					$item['inbox_date_posted']		= date('Y-m-d H:i:s');
					$item['inbox_status'] 			= 7;
					$item['inbox_sender'] 			= 'HN';
					$item['inbox_message_header'] 	= $this->validation->message_header;	
					$item['inbox_message_text'] 	= $this->validation->message_text;				
					
	       			if ($this->inbox_model->insert($item))
	       			{	
	         			redirect($this->curl); 
	       			}				
				}
			}
			
			$this->data['Title'] = $this->data['Title'].'/Send Message';
		
		} else {
			$this->view = $this->curl;
		}
		$this->renderLayout();
	}
}
?>